package com.tao.handler.security;

import com.alibaba.fastjson.JSON;

import com.tao.enums.AppHttpCodeEnum;
import com.tao.utils.WebUtils;
import com.tao.vo.ResponseResult;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author 季伟
 * 功能：当在验证时候有权限访问一个资源时如果发生发异常，
 * ExceptionTranslationFilter异常处理过滤器就会调用该类的handler方法处理异常
 */
@Component
public class AccessDinedHandlerImpl implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException {
        accessDeniedException.printStackTrace();
        //NO_OPERATOR_AUTH(403,"无权限操作")
        ResponseResult responseResult = ResponseResult.errorResult(AppHttpCodeEnum.NO_OPERATOR_AUTH);
        String json = JSON.toJSONString(responseResult);
        WebUtils.writeJsonDataToResponse(response,json);
    }
}
